<?php

namespace app\controller;

use app\model\Comment;
use think\Controller;
use think\Cookie;
use think\Request;

class Article extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return view();
    }

    /**
     * 文章列表获取
     * @param Request $request
     * @param $page
     * @return \think\response\Json
     */
    public function getcontent( Request $request, $page )
    {

        if ( $page ) {

            $where = [ 'md' => [ 'GT', date( 'H-y-d H:i:s' ) ], 'status' => 1 ];

            //拼接搜索条件
            $class = $request->get( 'class' );

            $time = $request->get( 'date' );

            if ( $class ) {

                $where[ 'cid' ] = $class;

            }

            if ( $time ) {

                $where['md'] = ['between',[$time.' 00:00:00',$time.' 23:59:59']];

            }

            $list = Db( 'article' )->where( $where )->page( $page, 15 )->select();

            if ( $list ) {

                $return = [ 'status' => true ];

                foreach ( $list as $key => $value ) {

                    $value[ 'content' ] = msubstr( getArtListContet( $value[ 'content' ] ), 0, 100, "utf-8" );

                    $return[ 'data' ][] = $value;

                }

            } else {

                $return = [ 'status' => false, 'info' => '没有更多的数据...' ];

            }

        } else {

            $return = [ 'status' => false, 'info' => '请求参数错误...' ];

        }
        // 渲染模板输出
        return json( $return );
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read( Request $request, $id )
    {

        if ( !$id ) {
            $this->error( 'url参数错误' );
        } else {
            return view();

        }
        //
    }

    /**
     * 获取文章详情方法
     * @param Request $request
     * @param $id
     * @return \think\response\Json
     */
    public function getarticle( Request $request, $id )
    {

        if ( $id ) {


            $article = db( 'article' )->where( [ 'status' => 1, 'cd' => [ 'GT', date( 'H-y-d H:i:s' ) ] ] )->find( $id );

            if ( $article ) {

                db( 'article' )->where( 'id', $id )->setInc( 'browse_num' );

                $return = [ 'status' => true, 'info' => $article ];

            } else {

                $return = [ 'status' => false, 'info' => '没有找到这篇文章' ];

            }

        } else {

            $return = [ 'status' => false, 'info' => '缺少必要参数' ];

        }

        return json( $return );
    }

    /**
     * 文章详情页获取label
     * @param Request $request
     * @param $label
     * @return \think\response\Json
     */
    public function getlabel( Request $request, $label )
    {

        if ( $label ) {

            $labelList = db( 'label' )->where( [ 'id' => [ 'IN', $label ] ] )->select();

            if ( $labelList ) {

                $return = [ 'status' => true, 'info' => $labelList ];


            } else {

                $return = [ 'status' => false, 'info' => '没有指定的标签' ];

            }

        } else {

            $return = [ 'status' => false, 'info' => '没有指定标签参数' ];

        }

        return json( $return );

    }

    /**
     * 新建评论
     * @param Request $request
     * @return \think\response\Json
     */
    public function mescreat( Request $request )
    {

        //拆解数组
        $name = $request->post( 'name' );
        $content = htmlspecialchars($request->post( 'content' ));
        $id = $request->post( 'id' );
        $email = $request->post( 'email' );

        if ( $name && $content && $id ) {

            //判断Email格式是否正确
            if ( $email ) {
                if ( checkEmail( $email ) ) {
                    $emailInfo = true;
                } else {
                    $emailInfo = false;
                }
            } else {
                $emailInfo = true;

            }

            //判断名字的长度
            if ( iconv_strlen( $name, 'utf-8' ) <= 10 ) {

                if ( $emailInfo ) {

                    $mesModel = new Comment();

                    $re = $mesModel->creatMes( $name, $content, $id, $email );

                    if ( $re ) {

                        $return = [ 'status' => true, 'info' => [ 'name' => $name, 'content' => $content, 'cd' => $mesModel->cd ] ];

                    } else {

                        $return = [ 'status' => false, 'info' => '数据写入失败,请重试' ];

                    }

                } else {

                    $return = [ 'status' => false, 'info' => '邮箱都填了,至少格式写个正确的吧' ];

                }

            } else {

                $return = [ 'status' => false, 'info' => '名字的长度不要超过10个字' ];

            }

        } else {

            $return = [ 'status' => false, 'info' => '参数上传不完整' ];
        }

        return json( $return );

    }

    /**
     * 获取评论内容
     * @param Request $request
     * @return \think\response\Json
     */
    public function getmes( Request $request )
    {

        //获取参数
        $page = $request->get( 'page' );

        $aid = $request->get( 'id' );

        if ( $page && $aid ) {

            $re = db( 'comment' )->where( [ 'status' => 1, 'aid' => $aid, ] )->field( 'id,cd,name,content' )->order( 'cd desc' )->page( $page, 10 )->select();

            if ( $re ) {

                $return = [ 'status' => true, 'info' => $re ];

            } else {

                $return = [ 'status' => false, 'info' => '没有更多评论...' ];

            }

        } else {

            $return = [ 'status' => false, 'info' => '参数错误' ];

        }

        return json( $return );

    }

    /**
     * 文章点赞
     * @param Request $request
     * @param $id
     * @return array
     */
    public function likearticle( Request $request, $id )
    {
        if ( $id ) {

            Cookie::init( [ 'prefix' => 'think_', 'expire' => 3600, 'path' => '/' ] );

            $isLiked = Cookie::get( 'isLiked' );

            if ( $isLiked ) {

                $return = [ 'status' => false, 'info' => '已经赞过这篇文章了' ];

            } else {

                $re = db( 'article' )->where( 'id', $id )->setInc( 'like_num' );

                if ( $re ) {

                    $return = [ 'status' => true, 'info' => '谢谢你愿意喜欢这篇文章' ];

                    Cookie::set( 'isLiked', true, 'forever' );

                } else {

                    $return = [ 'status' => false, 'info' => '数据更新失败了，要不重新试试' ];

                }

            }

        } else {

            $return = [ 'status' => false, 'info' => '参数错误' ];

        }

        return $return;
    }

    /**
     * 获取有文章的分类列表
     * @return array
     */
    public function getclass()
    {

        $re = db( 'articleClass' )->alias( 'c' )->where( [ 'a.status' => 1 ] )->join( '__ARTICLE__ a ', ' a.cid = c.id' )->group( 'c.id' )->field( 'c.id,c.name' )->select();

        if ( $re ) {

            $return = [ 'status' => true, 'info' => $re ];

        } else {

            $return = [ 'status' => false ];

        }

        return json( $return );

    }

    /**
     * 获取文章的时间搜索
     * @return \think\response\Json
     */
    public function getdate()
    {
        $model  = db( 'article' );
        $re = $model->where( [ 'status' => 1 ] )->field( 'cd' )->select();

        if ( $re ) {

            $date = [];

            //转换时间为单纯日期
            foreach ( $re as $v ) {

                $time = date( 'Y-m-d', strtotime( $v[ 'cd' ] ) );

                if(!in_array($time,$date)){

                    $date[] = $time;

                }

            }

            $return = [ 'status' => true, 'info' => $date ];

        } else {

            $return = [ 'status' => false ];

        }

        return json( $return );

    }
}
